//
//  HomeRecommendV.m
//  YangWifi
//
//  Created by zuoxueliangduyuanzhuo on 2017/6/14.
//  Copyright © 2017年 zuoxueliangduyuanzhuo. All rights reserved.
//

#import "HomeRecommendV.h"
#import "HomeRecommendCell.h"
#import "ProductInfoList.h"
@interface HomeRecommendV ()<UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic ,weak) IBOutlet UICollectionView *colV;

@property (nonatomic ,strong) NSMutableArray *data;

@end

@implementation HomeRecommendV

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self)
    {
        //you init
//        [self.colV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    }
    return self;
}

- (void)buildData
{

    ProductInfoList *list = [ProductInfoList mj_objectWithKeyValues:[AccountHanler packageList]];
    [self.data removeAllObjects];
    [self.data addObjectsFromArray:list.responseEntity];
    [self.colV reloadData];
}

#pragma mark -
#pragma mark - collectionDelegate


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.data.count;
}

// 定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH, 96);
}

// 定义每个UICollectionView 的 margin（内边距）
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
//    NSString *CellIdentifier = @"";
    HomeRecommendCell *cell = (HomeRecommendCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeRecommendCell" forIndexPath:indexPath];
    cell.data = self.data[indexPath.row];
    @WeakObj(self);
    cell.recommendPayHandler = ^(ProductInfoModel *infoModel) {
      
        if (Weakself.recommendHandler) {
            Weakself.recommendHandler(infoModel);
        }
    };
    cell.morePackageHandler = ^{
        if (Weakself.morePackageHandler) {
            Weakself.morePackageHandler();
        }
    };
    
    return cell;
}


// UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}


- (NSMutableArray *)data
{
    if (!_data) {
        _data = [NSMutableArray arrayWithCapacity:0];
    }
    return _data;
}

+ (HomeRecommendV *)instanceHomeRecommendView
{
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"HomeRecommendV" owner:self options:nil];
    return [nibView objectAtIndex:0];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.colV registerNib:[UINib nibWithNibName:@"HomeRecommendCell" bundle:nil] forCellWithReuseIdentifier:@"HomeRecommendCell"];

}

@end
